package com.welink.cmp.meatdata.command;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.welink.cmp.convertor.GenericConvertor;
import com.welink.cmp.dao.metadata.SwitchDaoService;
import com.welink.cmp.dao.metadata.dataobject.SwitchDO;
import com.welink.cmp.dto.metadata.MetadataIdListQry;
import com.welink.cmp.dto.metadata.switchs.CreateSwitchAddCmd;
import com.welink.cmp.dto.metadata.switchs.clientobject.SwitchBasicCO;
import com.welink.cmp.dto.metadata.switchs.query.SwitchListByPageQry;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.management.remote.rmi._RMIConnection_Stub;
import java.util.List;

/**
 * @author yanwei li create  2024-05-31 18:36
 */
@Component
public class SwitchCmdExe {

    @Resource
    private SwitchDaoService switchDaoService;

    /**
     * 保存Router信息
     *
     * @param addCmd 添加Router的命令对象
     * @return 新增Router的ID
     */
    public Long save(CreateSwitchAddCmd addCmd) {
        SwitchDO switchDO = new SwitchDO();
        BeanUtils.copyProperties(addCmd, switchDO);
        if (addCmd.isNeedsOperator()){
            switchDO.setCreateBy(addCmd.getOperator());
        }
        switchDaoService.save(switchDO);
        return switchDO.getId();
    }

    /**
     * 更新Router信息
     *
     * @param updDo 更新Router的命令对象
     * @return 更新是否成功
     */
    public Boolean update(SwitchDO updDo) {
        return switchDaoService.updateById(updDo);
    }

    /**
     * 根据ID查询Router信息
     *
     * @param id Router ID
     * @return Router业务对象
     */
    public SwitchBasicCO queryById(Long id) {
        SwitchDO switchDO = switchDaoService.getById(id);
        SwitchBasicCO routerCO = new SwitchBasicCO();
        BeanUtils.copyProperties(switchDO, routerCO);
        return routerCO;
    }

    /**
     * 列表查询Router信息
     *
     * @param listQry 列表查询条件
     * @return Router业务对象列表
     */
    public List<SwitchBasicCO> listQuery(MetadataIdListQry listQry) {
        LambdaQueryWrapper<SwitchDO> wrapper = new LambdaQueryWrapper<>();
        // 根据列表查询条件设置查询条件

        final List<Long> idList = listQry.getIdList();
        List<SwitchDO> switchDOList = switchDaoService.listByIds(idList);
        return GenericConvertor.toCOList(switchDOList, SwitchBasicCO.class);
    }

    /**
     * 分页查询Router信息
     *
     * @param pageQry 分页查询条件
     * @return 分页响应，包括Router列表和分页信息
     */
    public Page<SwitchDO> pageQuery(SwitchListByPageQry pageQry) {
        return switchDaoService.pageQry(pageQry);
    }

}
